package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.base.isMobile;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2016/12/2.
 */

public class returnPassword extends AppCompatActivity implements View.OnClickListener{
    private EditText ReturnPasswordTelephone;
    private EditText ReturnPasswordCode;
    private EditText ReturnPassword;
    private EditText ReturnEnterPassword;
    private Button   buttonReturnCode,buttonLand;
    private String Telephone,Password,EnterPassword,Code;
    //声明一个SharedPreferences对象和一个Editor对象
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private AsyncHttpClient client=new AsyncHttpClient();
    private RequestParams params=new RequestParams();
    private boolean runningThree;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.return_passwprd);
        initView();
    }
    public void initView(){
        TextChange textChange=new TextChange();
        ReturnPasswordTelephone=(EditText)findViewById(R.id.returenPassword_telepone);
        ReturnPasswordTelephone.addTextChangedListener(textChange);
        ReturnPasswordCode=(EditText)findViewById(R.id.returenPassword_Code);
        ReturnPasswordCode.addTextChangedListener(textChange);
        ReturnPassword=(EditText)findViewById(R.id.returenPassword_Password);
        ReturnPassword.addTextChangedListener(textChange);
        ReturnEnterPassword=(EditText)findViewById(R.id.returenPassword_Enter_Password);
        ReturnEnterPassword.addTextChangedListener(textChange);
        buttonReturnCode=(Button)findViewById(R.id.returenPassword_code_Button);
        buttonReturnCode.setOnClickListener(this);
        buttonLand=(Button)findViewById(R.id.returenPassword_Button);
        buttonLand.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.returenPassword_code_Button:
                isMobile mobile=new isMobile(Telephone);
                params.put("phone_no", Telephone);
                if(mobile.isMobileNO(Telephone)==false){
                    Toast.makeText(returnPassword.this, "手机号格式不正确", Toast.LENGTH_LONG).show();
                }else {
                    RequestHandle request=client.post(Urls.returnPassword_Code,params,new JsonHttpResponseHandler(){
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            String jsonStr=response.toString();
                            JSONObject jsonObject = null;
                            JSONArray jsonArray=null;
                            try {
                                jsonObject = new JSONObject(jsonStr);
                                String msg=jsonObject.getString("msg");
                                Toast.makeText(returnPassword.this,msg,Toast.LENGTH_LONG).show();
                                JSONObject data=jsonObject.getJSONObject("data");
                                String out_time=data.getString("out_time");
                                int time=Integer.valueOf(out_time).intValue();
                                CountDownTimer downTimer=new CountDownTimer(time*1000,1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        runningThree = true;
                                        buttonReturnCode.setText((millisUntilFinished / 1000) + "秒之后重新发送");
                                    }

                                    @Override
                                    public void onFinish() {
                                        runningThree = false;
                                        buttonReturnCode.setText("重新发送");
                                    }
                                };

                                if (runningThree) {
                                } else {
                                    downTimer.start();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }


                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            super.onFailure(statusCode, headers, responseString, throwable);
                        }
                    });
                    break;
                }
            case R.id.returenPassword_Button:
                if (Password.length() < 6) {
                    Toast.makeText(returnPassword.this, "请输入6-18位密码", Toast.LENGTH_LONG).show();
                }else if (Password.equals(EnterPassword)) {
                    //获取preferences和editor对象
                    preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
                    editor = preferences.edit();
                    editor.putString("userName",Telephone);
                    editor.putString("passWord",Password);
                    editor.commit();


                    params.put("rand_code", Code);
                    params.put("phone_no", Telephone);
                    params.put("new_pwd", Password);
                    params.put("confirm_pwd", EnterPassword);
                    RequestHandle handle = client.post(Urls.returnPassword, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            String jsonStr = response.toString();
                            try {
                                JSONObject jsonObject = new JSONObject(jsonStr);
                                String msg = jsonObject.getString("msg");
                                Toast.makeText(returnPassword.this, msg, Toast.LENGTH_LONG).show();
                                String code = jsonObject.getString("code");
                                int Intcode=Integer.valueOf(code).intValue();
                                if(Intcode==1){
                                    startActivity(new Intent(returnPassword.this,Land.class));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            super.onFailure(statusCode, headers, responseString, throwable);
                            Log.e("fail", responseString);
                        }
                    });
                } else {
                    Toast.makeText(returnPassword.this, "密码输入不一致", Toast.LENGTH_LONG).show();
                }


                break;
        }
    }

    //EditText的监听器
    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            Telephone=ReturnPasswordTelephone.getText().toString();
            Code=ReturnPasswordCode.getText().toString();
            Password=ReturnPassword.getText().toString();
            EnterPassword=ReturnEnterPassword.getText().toString();

        }
    }
}
